import org.snmp4j.*;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.PDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

import java.io.IOException;
import java.util.List;

/**
 * @author SangLiang on 2018/6/19.
 * @version 1.0
 */



public class SnmpTest {

    private static String[] oids = {
             "1.3.6.1.2.1.25.5.1.1.1" //进程时间片
            //"1.3.6.1.2.1.25.3.3.1.2"  //每个核心负载
            //"1.3.6.1.2.1.1.1" //系统信息
            //,
            //"1.3.6.1.4.1.2021.11.50",
            //"1.3.6.1.4.1.2021.11.51",
            //"1.3.6.1.4.1.2021.11.52",
            //"1.3.6.1.4.1.2021.11.53",
            //"1.3.6.1.2.1.25.5.1.1.1"
    };


    private static String targetAddr ="udp:192.168.1.168/161";
    private static String targetPid = "6792";


    public static void main(String[] args) {
        try {
            while (true) {
                CpuInfo cpuInfo1 = collectCPU();
               /* if(null!=cpuInfo1) {
                    System.out.println("process:" + cpuInfo1.getProcess() + " total:" + cpuInfo1.getTotal());
                }*/
                System.out.println("process:" + cpuInfo1.getProcess() + " total:" + cpuInfo1.getTotal());
                Thread.sleep(1000);
            }

        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
    }



    //获取cpu使用率
    public static CpuInfo collectCPU() {
        CpuInfo cpuInfo = new CpuInfo();

        TransportMapping transport = null ;
        Snmp snmp = null ;
        CommunityTarget target;

        try {
            transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            snmp.listen();
            target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));
            target.setRetries(2);
            target.setAddress(GenericAddress.parse(targetAddr));
            target.setTimeout(8000);
            target.setVersion(SnmpConstants.version2c);
            TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {
                @Override
                public PDU createPDU(Target arg0) {
                    PDU request = new PDU();
                    request.setType(PDU.GET);
                    return request;
                }

                @Override
                public PDU createPDU(MessageProcessingModel messageProcessingModel) {
                    System.out.println(messageProcessingModel.getID());
                    return null;
                }
            });
            OID[] columns = new OID[oids.length];
            for (int i = 0; i < oids.length; i++)
                columns[i] = new OID(oids[i]);
            List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
            if(null!=list && list.size()>0){
                Long totalTime =0L;
                for(TableEvent event : list){
                    Variable variable = event.getColumns()[0].getVariable();
                    totalTime += variable.toLong();
                }
                cpuInfo.setTotal(totalTime);

                for(TableEvent event : list){
                    OID oid= event.getIndex();
                    if(oid.toString().equalsIgnoreCase(targetPid)){
                        Variable variable = event.getColumns()[0].getVariable();
                        cpuInfo.setProcess(variable.toLong());
                        return cpuInfo;
                    }

                }
            }

        } catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(transport!=null)
                    transport.close();
                if(snmp!=null)
                    snmp.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        return null;
    }
}

class CpuInfo{
    private Long total;
    private Long process;

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getProcess() {
        return process;
    }

    public void setProcess(Long process) {
        this.process = process;
    }
}
